package com.baosight.sczx.zm.cn.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.sczx.zm.cn.domain.ZMCNMcmodule;
import com.baosight.sczx.zm.cn.domain.ZMCNWbccyMain;
import com.baosight.sczx.zm.cn.domain.ZMCNYdzhcyMain;
import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;

/**
 * 外部抽查样主表
 * <p>Title: ServiceZMCN30.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-27 上午08:01:23  严洪浩  Create
 *</pre>
 */
public class ServiceZMCN31 extends ServiceEPBase {
	/**
	 * 初始进入方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCN10</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:37:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(getCoalCode());
//		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
		return outInfo;
	}
	
	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCN31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:47:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
//		StaticTools.setQueryParameters(inInfo, "fdBjdw", UserSession.getInSessionProperty("miningCode").toString(), true);
		EiInfo outInfo = super.query(inInfo, "ZMCNWbccyMain.query", new ZMCNWbccyMain());
		
		outInfo.addBlock(getPartDept()); 
		outInfo.addBlock(StaticBlocks.getStauts4());
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", false));//班次
//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
		//以下102 为松藻煤电公司id，如果有变化，只需要修改下边的id即可
//		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,"SZ008"));
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		outInfo.addBlock(getCoalCode());
		return outInfo;
	}

	/**
	 * 煤样编码下拉框
	 * @return
	 */
	public EiBlock getCoalCode() {
		EiBlock block = new EiBlock("cmes.ch.coalcode");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			Map<String, String> params=new HashMap();
			params.put("fdStatus", "生效");
			_listData = dao.query("ZMCNMcmodule.query", params);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_map = new HashMap<String, String>();
			ZMCNMcmodule _temp = (ZMCNMcmodule)_listData.get(i);
			_map.put("value", _temp.getFdMyCode());
			_map.put("display",_temp.getFdMyDetail());
			_list.add(_map);
		} 
		block.setRows(_list);
		return block;
	}
	
	/**
	 * 得到质检中心下属分站下拉框
	 * @return
	 */
	public EiBlock getPartDept() {
		EiBlock block = new EiBlock("cmes.ch.partdept");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);
		
		_map = new HashMap<String, String>();
		_map.put("value", "fz001");
		_map.put("display", "中心站");
		_list.add(_map);
		_map = new HashMap<String, String>();
		_map.put("value", "fz002");
		_map.put("display", "松藻站");
		_list.add(_map);
		_map = new HashMap<String, String>();
		_map.put("value", "fz003");
		_map.put("display", "白岩站");
		_list.add(_map);
		_map = new HashMap<String, String>();
		_map.put("value", "fz004");
		_map.put("display", "金鸡岩站");
		_list.add(_map);

		block.setRows(_list);
		return block;
	}
//	public EiBlock getMines() {
//	EiBlock block = new EiBlock("cmes.ch.mine");
//
//	EiColumn EiColumn = new EiColumn("display");
//	EiColumn.setType("C");
//	block.addMeta(EiColumn);
//
//	EiColumn = new EiColumn("value");
//	EiColumn.setType("C");
//	block.addMeta(EiColumn);
//
//	List<Map> _list = new ArrayList<Map>();
//	Map<String, String> _map = new HashMap<String, String>();
//	_map.put("value", "");
//	_map.put("display", "");
//	_list.add(_map);
//
//	List<Map> _listData = new ArrayList<Map>();
//	try {
//		_listData = dao.query("ZMCNWbccyMain.queryTreeChild", null);
//	} catch (Exception e) {}
//	
//	for(int i = 0; i < _listData.size(); i++) {
//		_list.add(_listData.get(i)); 
//	} 
//	block.setRows(_list);
//	return block;
//}

	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25上午11:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo(); 
		ZMCNWbccyMain _dom = new ZMCNWbccyMain();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CN_WBCCY_MAIN"));
			this.dao.insert("ZMCNWbccyMain.insert", _dom);
	    }
	 	out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
				
 }

	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-24上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo();
		ZMCNWbccyMain _dom = new ZMCNWbccyMain();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
		    this.dao.update("ZMCNWbccyMain.update", _dom);			
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
	
}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-26上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{

		EiInfo outInfo = null;
		
		List<String> _list = new ArrayList<String>();
		Map<String,List<String>> params = new HashMap<String,List<String>>();

		for(int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++)
		{

			EiInfo _temp = super.delete(inInfo, "ZMCNWbccyMain.delete");
	        outInfo = this.query(inInfo);
	        outInfo.setMsg(_temp.getMsg());
	        outInfo.setDetailMsg(_temp.getDetailMsg());
		}
		
		return outInfo;
	}
	
	/**
	 * 上报
	 * <p>Function: report</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午04:07:27 张志芳
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCNWbccyMain _dom = new ZMCNWbccyMain();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());

			this.dao.update("ZMCNWbccyMain.report",_dom);
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	
	}
	

	/**
	 * 校验是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:48:34 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo validate(EiInfo inInfo) {
		ZMCNWbccyMain _dom = new ZMCNWbccyMain();
		String oper=(String)inInfo.get("efCurButtonEname");
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			String fdMonth = _dom.getFdMonth();
			String fdYear = _dom.getFdYear();
			String fdBjdw = _dom.getFdBjdw();
			if(oper.equalsIgnoreCase("update1")||oper.equalsIgnoreCase("update"))
			{
				params.put("fdKeyId", _dom.getFdKeyId());
			}
			params.put("fdMonth", fdMonth);
			params.put("fdYear", fdYear);
			params.put("fdBjdw", fdBjdw);
			
			List result = dao.query("ZMCNWbccyMain.validate", params);
	//		EiInfo out=new EiInfo();
			if(result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				ZMCNWbccyMain _temp = (ZMCNWbccyMain)result.get(0);
			
				StringBuffer msg = new StringBuffer("提交失败，");
				
				if(_temp.getFdYear() != null && _temp.getFdYear().equals(fdYear)) {
					msg.append(" 年份 ");
				}
				if(_temp.getFdMonth() != null && _temp.getFdMonth().equals(fdMonth)){
					msg.append(" 月份");
				}
				if(_temp.getFdBjdw() != null && _temp.getFdBjdw().equals(fdBjdw)) {
					msg.append(" 被检单位");
				}
//				if(_temp.getFdCcDate() != null && _temp.getFdCcDate().equals(fdCcDate)) {
//					msg.append(" 抽查日期 ");
//				}
	
			msg.append("重复！");
			
			inInfo.setMsg(msg.toString());
				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}
	
	/**
	 * 校验是否可以操作
	 * <p>Function: validate1</p>
	 * <p>Description: 明细上报不能修改主表，有明细不能删除主表。</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-8-23 17:09:36 lhx
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo validate1(EiInfo inInfo) {
		ZMCNWbccyMain _dom = new ZMCNWbccyMain();
		String oper=(String)inInfo.get("efCurButtonEname");
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			String fdForeignId = _dom.getFdKeyId();
			if(oper.equalsIgnoreCase("update1")||oper.equalsIgnoreCase("update"))
			{
				params.put("fdStatus", "上报");
			}
			params.put("fdForeignId", fdForeignId);
			
			List result = dao.query("ZMCNWbccy.validate1", params);
	//		EiInfo out=new EiInfo();
			if(result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				StringBuffer msg = new StringBuffer("提交失败，");
				if(oper.equalsIgnoreCase("update1")||oper.equalsIgnoreCase("update"))
				{
					msg.append("明细已经上报，不能修改主表！");
				}else{
					msg.append("明细有记录。为了防止误删除，请先删除明细后再删除主表！");
				}
			inInfo.setMsg(msg.toString());
				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}
}
